import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular';
//添加银行卡
import { BankcardPage } from '../bankcard/bankcard';
//设置支付密码
import { SetpaypwdPage } from '../setpaypwd/setpaypwd';
import { Https } from '../../app/http.service'
/**
 * Generated class for the WithdrawcashPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-withdrawcash',
  templateUrl: 'withdrawcash.html',
})
export class WithdrawcashPage {
	balanceUrl = new Https().Url + "act=goods&op=app_my_balance"
	checkPwdUrl = new Https().Url + "act=goods&op=app_check_pay_pwd";
	withdrawcashUrl = new Https().Url + "act=goods&op=app_tixian"
	drawCashMax:string = "0.00";
	bank: string
	bankInfo = []
	state:boolean
	pwd = [];
	active = 0;
	pcactive: any;
	bank_id:string
	constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController) {
		this.bank_id = this.navParams.get('bank_id') == undefined ? 0 : this.navParams.get('bank_id')
	}

	ionViewWillEnter() {
		console.log("WithdrawcashPage")
		let then = this
		fetch(this.balanceUrl, {
			method: "post",
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&bank_id=" + then.bank_id,
		}).then((response) => {
			response.text().then((con) => {
				let resource = JSON.parse(con);
				then.drawCashMax = (resource['predeposit'] - 2).toString();
				then.bankInfo = resource['bank_info']
				then.bank_id = resource['bank_info']['pdc_id']
				then.bank = resource['bank_info']['code']
				
			})
		}).catch((err) => {
			let confirm = then.alertCtrl.create({
				title: '温馨提示',
				message: err,
				buttons: [
					{ text: '确定' },

				]
			});
			confirm.present();
		})
	}
	openNewWindow(){
		this.navCtrl.pop();
		this.navCtrl.push(BankcardPage);
	}

	allCash($eve){
		$eve.value = this.drawCashMax;
	}
	inputState(input){
		let then = this;
		console.log(input.value)
		then.state = then.bank == '1' && input.value != '' ? true : false
	}
	next(input){
		let then = this;
		console.log(input.value)
		
		if (parseFloat(input.value) > parseFloat(then.drawCashMax)){
			let confirm = then.alertCtrl.create({
				title: '温馨提示',
				message: "提现金额超过您的余额",
				buttons: [
					{ text: '确定' },

				]
			});
			confirm.present();
		}else{
			//extract_money
			fetch(this.withdrawcashUrl, {
				method: "post",
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&bank_id=" + then.bank_id + "&extract_money=" + input.value,
			}).then((response) => {
				response.text().then((con) => {
					let resource = JSON.parse(con);
					if (resource.datas == "3"){
						let confirm = then.alertCtrl.create({
							title: '温馨提示',
							message: "提现申请成功",
							buttons: [
								{ text: '确定' },

							]
						});
						confirm.present();
						this.navCtrl.pop()
					}else{
						let confirm = then.alertCtrl.create({
							title: '温馨提示',
							message: "提现申请失败",
							buttons: [
								{ text: '确定' },

							]
						});
						confirm.present();
						this.navCtrl.pop()
					}
				})
			}).catch((err) => {
				let confirm = then.alertCtrl.create({
					title: '温馨提示',
					message: err,
					buttons: [
						{ text: '确定' },

					]
				});
				confirm.present();
			})
		}
		
	}

	confirmPay(input) {
		let then = this;
		if (input.value == '' || input.value == null) {
			return
		}
		if (parseInt(localStorage.getItem("payPwd")) == 0 || localStorage.getItem("payPwd") == null) {
			let confirm = then.alertCtrl.create({
				title: '温馨提示',
				message: "您尚未设置支付密码，请前往设置",
				buttons: [
					{
						text: '确定',
						handler: () => {
							this.navCtrl.push(SetpaypwdPage)
						},
					},
					{ text: '取消' }
				]
			});
			confirm.present();
			return false;
		}
		let alert = this.alertCtrl.create({
			cssClass: " password ",
			enableBackdropDismiss: false,
		});

		alert.present();
		setTimeout(() => {
			//弹窗主体
			let passPop = document.querySelector(".password>.alert-wrapper")
			//弹窗内容
			passPop.innerHTML = '<div class="pord" ><div class="title" ><img src="./assets/imgs/xx.png"></img><span>请输入支付密码</span></div><div class="tixian"><span class="tixiantitle">提现</span><span class="money">￥' + (parseInt(input.value)).toFixed(2) +'</span><span class="withdraw">手续费￥2元</span> </div><div class="hidden-pwd"  (keyup)="backkey(int,$event)" (keypress)="intkey(int,$event)"><input pattern="[0-9]*"  type="number" class="number"></div><div class="input" ><div class="pan" #int ><span contenteditable="true" ></span><span contenteditable="true" ></span><span contenteditable="true" ></span><span contenteditable="true" ></span><span contenteditable="true" ></span><span contenteditable="true" ></span></div><span class="remember"><a>忘记密码</a></span></div></div>'
			//获取关闭X的点击
			let close = document.querySelector(".password>.alert-wrapper>.pord>.title>img")
			//忘记密码
			let remember = document.querySelector(".password>.alert-wrapper>.pord>.input>.remember>a")
			//输入框
			let pwdInput = document.querySelector(".password>.alert-wrapper>.pord>.input>.pan")
			let pwdHiddenInput = document.querySelector(".password>.alert-wrapper>.pord>.hidden-pwd>input")

			close.addEventListener("click", () => {
				//关闭弹窗
				this.pwd.splice(0, this.pwd.length)
				console.log(this.pwd)
				this.active = 0
				alert.dismiss();
			})
			remember.addEventListener("click", () => {
				//忘记密码跳转
				console.log(remember)
			})
			pwdHiddenInput.addEventListener("keyup", (event: any) => {
				// console.log(event.keyCode, 'keyCode')
				//输入密码
				if (event.keyCode == 8 && this.active != 0) {
					this.active = this.pwd.length - 1 <= 0 ? 0 : this.pwd.length - 1;
					if (this.active >= 0) {
						pwdInput.children[this.active].className = " "
						this.pwd.pop()
						if (this.pwd.length == 1) {
							this.pwd = [];
							// this.active = 0;
							
						}
					}

				}
			})
			pwdHiddenInput.addEventListener("textInput", (event: any) => {
				event.preventDefault();
				//输入密码
				// console.log(event.originalEvent.data.charCodeAt(),'keyCode')
				// console.log(event.data,'keyCode')
				// console.log(event.charCode,'charCode')
				// this.pwd[this.active] = String.fromCharCode(event.charCode)
				this.pwd[this.active] = event.data
				if (this.pwd.length == 6) {
					console.log(this.pwd);
					let pay = this.pwd[0] + this.pwd[1] + this.pwd[2];
					pwdInput.children[this.pwd.length - 1].className = "circle"
					fetch(this.checkPwdUrl, {
						method: "post",
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&pay=" + pay + "&pwn=" + this.pwd[3] + this.pwd[4] + this.pwd[5],
					}).then((response) => {
						response.text().then((json) => {
							if (json != '') {
								let data = JSON.parse(json).datas;
								if (data.code == '0') {
									then.next(input)
									this.pwd = [];
									this.active = 0;
									alert.dismiss();
								} else {
									let confirm = then.alertCtrl.create({
										title: '温馨提示',
										message: "密码输入错误，请重新输入",
										buttons: [
											{ text: '确定' },

										]
									});
									confirm.present();
								}
							} else {
								let confirm = then.alertCtrl.create({
									title: '温馨提示',
									message: "网络连接错误，请重试",
									buttons: [
										{ text: '确定' },

									]
								});
								confirm.present();
							}
						})
					}).catch((err) => {
						let confirm = then.alertCtrl.create({
							title: '温馨提示',
							message: err,
							buttons: [
								{ text: '确定' },

							]
						});
						confirm.present();
					})


				} else if (this.pwd.length < 6) {
					pwdInput.children[this.pwd.length - 1].className = "circle"
					this.active++;
					this.pcactive = pwdInput.children[this.pwd.length];
					// this.pcactive.focus()
				}
			})

		}, 100)

	}

}
